#!/usr/bin/env bash
# bash <(curl -sLk https://gitee.com/uon/script/raw/master/shell/synology/synology_cloudflareddns.sh)

confinf="服务供应商 选择Cloudflare
主机名称:   填写 DDNS 域名
用户名  :   填写区域 ID
密码    :   填写上文生成的 API 令牌"

if ! uname -a | grep -Eqi synology; then
    echo "不支持的系统！"
    exit
fi

function install() {
    curl -fLk --retry 3 https://raw.githubusercontent.com/joshuaavalon/SynologyCloudflareDDNS/master/cloudflareddns.sh -o /sbin/cloudflareddns.sh && echo "cloudflareddns.sh 脚本下载完毕"
    sed -ri 's/proxy=".*"/proxy="false"/g' /sbin/cloudflareddns.sh
    chmod +x /sbin/cloudflareddns.sh

    sed -ri '/\[Cloudflare\]/,/website\=https:\/\/www\.cloudflare\.com\n/d' /etc.defaults/ddns_provider.conf && echo "ddns_provider.conf 配置添加成功"
    sed -ri '/^\s*$/d' /etc.defaults/ddns_provider.conf
    cat >>/etc.defaults/ddns_provider.conf <<EOF

[Cloudflare]
        modulepath=/sbin/cloudflareddns.sh
        queryurl=https://www.cloudflare.com
        website=https://www.cloudflare.com
EOF
    sed -ri '/^\s*$/d' /etc.defaults/ddns_provider.conf
    echo "${confinf}"

}

function uninstall() {
    sed -ri '/\[Cloudflare\]/,/website\=https:\/\/www\.cloudflare\.com\n/d' /etc.defaults/ddns_provider.conf && echo "ddns_provider.conf 配置移除成功"
    sed -ri '/^\s*$/d' /etc.defaults/ddns_provider.conf
    rm -f /sbin/cloudflareddns.sh && echo "cloudflareddns.sh 脚本移除成功"
}

if [ "$1" == "-u" ]; then
    uninstall
else
    install
fi
